<template>
  <div>
    <basic-container>
      <avue-tabs :option="option" v-model="form" @change="handleChange" @submit="handleSubmit"></avue-tabs>
    </basic-container>
  </div>
</template>

<script>
import option from "@/const/info/form";
import { mapGetters } from "vuex";
import { updateAdminInfo, updatePwd } from "@/api/admin";
export default {
  data() {
    return {
      type: "info",
      option: option,
      form: {}
    };
  },
  created() {
    this.handleWitch();
  },
  computed: {
    ...mapGetters(["userInfo"])
  },
  methods: {
    handleSubmit() {
      // this.$message({
      //   message: this.form,
      //   type: "success"
      // });
      if (this.type === "info") {
        updateAdminInfo(this.form).then(() => {
          this.showSuccess("修改成功");
          this.$store.dispatch("SetUserInfo", this.form);
        });
      } else {
        updatePwd(this.form).then(() => {
          this.showSuccess("修改成功,请使用新密码");
          this.form.oldPwd = "";
          this.form.newPwd = "";
          this.form.newPwd2 = "";
        });
      }
    },
    handleWitch() {
      if (this.type === "info") {
        this.form = {
          username: this.userInfo.username,
          realname: this.userInfo.realname,
          telephone: this.userInfo.telephone,
          sign: this.userInfo.sign
        };
      } else if (this.type === "password") {
        this.form = {
          oldPwd: "",
          newPwd: "",
          newPwd2: ""
        };
      }
    },
    handleChange(item) {
      this.type = item.prop;
      this.handleWitch();
    }
  }
};
</script>

<style>
</style>
